#!/usr/bin/env python3

# Copyright (C) 2013-2014 LiuLang <gsushzhsosgsu@gmail.com>

# Use of this source code is governed by GPLv3 license that can be found
# in the LICENSE file.

import sys
import dbus
from gi.repository import Gtk


BUS_NAME = 'org.mpris.MediaPlayer2.kwplayer'
MPRIS_PATH = '/org/mpris/MediaPlayer2'
ROOT_IFACE = 'org.mpris.MediaPlayer2'

def check_loaded():
    '''Check kwplayer is loaded or not.
    
    If kwplayer is running, raise kwplayer window and return True,
    otherwise return False.'''
    sb = dbus.SessionBus()
    try:
        kw_proxy = sb.get_object(BUS_NAME, MPRIS_PATH)
        prop = dbus.Interface(kw_proxy, ROOT_IFACE)
        prop.Raise()
        return True
    except dbus.exceptions.DBusException:
        return False

def load_app():
    from kuwo.App import App
    app = App()
    sys.exit(app.run(sys.argv))

def main():
    if not check_loaded():
        load_app()

if __name__ == '__main__':
    main()
